import java.util.LinkedList;
import java.util.Queue;

public class Solution {
    int m = 0, n = 0;
    Queue<int[]> q = new LinkedList<>();
    int[] dx = {0, 0, 1, -1};
    int[] dy = {1, -1, 0, 0};

    public int[][] highestPeak(int[][] isWater) {
        //0陆地，1水
        m = isWater.length;
        n = isWater[0].length;

        int[][] ret = new int[m][n];

        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (isWater[i][j] == 1) {
                    q.offer(new int[]{i, j});
                }
            }
        }

        boolean[][] flag = new boolean[m][n];

        while (!q.isEmpty()) {
            int[] arr = q.poll();
            int x = arr[0];
            int y = arr[1];
            flag[x][y] = true;
            for (int i = 0; i < 4; i++) {
                int xx = x + dx[i];
                int yy = y + dy[i];
                if (xx >= 0 && yy >= 0 && xx < m && yy < n && !flag[xx][yy] && isWater[xx][yy] != 1) {
                    ret[xx][yy] = ret[x][y] + 1;
                    q.offer(new int[]{xx, yy});
                    flag[xx][yy] = true;
                }
            }
        }

        return ret;
    }

    public static void main(String[] args) {
        int[][] arr = new int[][]{{0, 1}, {0, 1}};
        Solution solution = new Solution();
        System.out.println(solution.highestPeak(arr));
    }
}
